package com.llkj.qingnianguoxue;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;
import com.llkj.qingnianguoxue.base.ConstantUserInfo;
import com.llkj.qingnianguoxue.base.MyApplication;
import com.llkj.qingnianguoxue.login.LoginActivity;
import com.llkj.qingnianguoxue.util.AppManager;
import com.llkj.qingnianguoxue.util.NetworkUtils;

import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

public class DialogNotificationActivity extends FragmentActivity {
    public static final String LOGIN_OUT = "login_out";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_dialog_notification);
        TextView tvTitle = (TextView) findViewById(R.id.dialog_title);
        TextView tvContent = (TextView) findViewById(R.id.dialog_content);
        TextView rigntBtn = (TextView) findViewById(R.id.dialog_right_btn);

        Intent intent = getIntent();
        if (intent.hasExtra(ConstantUserInfo.ACCOUNT_CONFLICT) && intent.getBooleanExtra(ConstantUserInfo.ACCOUNT_CONFLICT, false)) {
            loginOut();

            tvTitle.setText("提示");
            tvContent.setText("您的账号在其他设备登录");
            tvContent.setVisibility(View.VISIBLE);
//            leftBtn.setText("取消");

            rigntBtn.setText("确定");
            MyApplication.getInstance().sputil.put(ConstantUserInfo.ISLOGIN, false);
            MyApplication.getInstance().sputil.put(ConstantUserInfo.USER_ID, "");
            rigntBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent intent = new Intent(DialogNotificationActivity.this, LoginActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("login_out", "1");
                    intent.putExtras(bundle);
                    startActivity(intent);
//                    AppManager.getInstance().finishAllActivityAndExit();
//                    AppManager.getInstance().finishOhterActivity(LoginActivity.class);
                    finish();
                }
            });
//            Intent locationService = new Intent(this, MyLocationService.class);
//            stopService(locationService);
        } else if (intent.hasExtra(LOGIN_OUT) && intent.getBooleanExtra(LOGIN_OUT, false)) {//
            loginOut();

            tvTitle.setText("提示");
            tvContent.setText("您的账号已被冻结或注销了");
            tvContent.setVisibility(View.VISIBLE);
//            leftBtn.setText("取消");

            rigntBtn.setText("确定");

            rigntBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(DialogNotificationActivity.this, LoginActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("login", "out");
                    intent.putExtras(bundle);
                    startActivity(intent);
                    AppManager.getInstance().finishAllActivityAndExit();
                    finish();
                }
            });
        }
    }

    private void loginOut() {
        JPushInterface.setAliasAndTags(getApplicationContext(), "", null, mAliasCallback);//极光退出
    }

    protected final TagAliasCallback mAliasCallback = new TagAliasCallback() {

        @Override
        public void gotResult(int code, String alias, Set<String> tags) {
            String logs;
            switch (code) {
                case 0:
                    logs = "Set tag and alias success";
                    Log.i("Tag", logs);
                    break;

                case 6002:
                    logs = "标签和别名设置失败.60s后自动重试";
                    Log.i("Tag", logs);
                    if (NetworkUtils.isNetworkConnected(getApplicationContext()) && mHandler != null) {
                        mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_SET_ALIAS, alias), 1000 * 60);
                    }
                    break;

                default:
                    logs = "Failed with errorCode = " + code;
                    Log.i("Tag", logs);
            }

        }

    };

    protected static final int MSG_SET_ALIAS = 1001;
    protected final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MSG_SET_ALIAS:
                    Log.d("Tag", "Set alias in handler.");
//                    JPushInterface.setAliasAndTags(getApplicationContext(), (String) msg.obj, null, mAliasCallback);
                    break;
                default:
            }
        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent intent = new Intent(DialogNotificationActivity.this, LoginActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("login", "out");
            intent.putExtras(bundle);
            startActivity(intent);
            finish();
        }
        return false;
    }
}
